﻿using System.Collections.Generic;
using ApplicationLibrary.DBManager;
using ApplicationLibrary.ExtendedControls;
using ApplicationLibrary.SqlConnector;
using ApplicationLibrary;
using Fighter.Masters.ModuleData;
using System.Windows.Forms;
using System.Data;

namespace Fighter.Masters.Forms
{
    public partial class Athlete : ApplicationLibrary.DBManager.DocumentForm
    {
        private BindableOject<int> ID = null;
        private EnumsManager<Gender> eGenderManager = new EnumsManager<Gender>();
        private EnumsManager<Belts> eBeltsManager = new EnumsManager<Belts>();

        public Athlete()
            : base(Properties.Resources.F_Club)
        {
            InitializeComponent();
            keyControl = txtAthlete;
        }

        #region Virtual Method

        protected override IRadarParameters GetKeyFromDocument()
        {
            return new RadarAthleteParam(ID.Value);
        }

        protected override void OnAttachData()
        {
            dbManager = new dbmanagerAthlete("AthleteForm", new RadarAthlete(), this);
            dbManager.AddMaster(this, FG_Athlete.Name, true);

            cbbClub.AttachDataReader<string>(new RRClub(), FG_Club.Mail, FG_Club.Club, true);
            eGenderManager = new EnumsManager<Gender>(cbbGender);
            eBeltsManager = new EnumsManager<Belts>(cbbBelt);

            //dbManager.AddSlave(FG_Coach.Name, true);
            //dbManager.AddRelation("Coach", FG_Club.Mail, FG_Coach.Mail, false);

            //cbbWeight.AttachDataReader<int>(new RRDesctiptionType(DescriptionType.E_Regions), FG_Descriptions.ID, FG_Descriptions.Description, true);

            ID = new BindableOject<int>();
            ID.AttachCounterType(Counters.Athlete.Int(), GlobalInfo.CurrentDate, null);
        }

        protected override void OnBindData()
        {
            BindControl(cbbClub, FG_Athlete.Mail, "SelectedValue", NullValue.NotSet);
            BindControl(txtAthlete, FG_Athlete.FirstName);
            BindControl(cbbGender, FG_Athlete.Gender, "SelectedValue", NullValue.NotSet);
            BindControl(ctbBirthday, FG_Athlete.BirthDay);
            BindControl(ntbWeight, FG_Athlete.Weight);
            BindControl(cbbBelt, FG_Athlete.Belt, "SelectedValue", NullValue.NotSet);
            BindControl(cbbGrapLevel, FG_Athlete.GrapLevel);
            BindControl(ntbFContMatch, FG_Athlete.FContMatch);
            BindControl(ntbLKickMatch, FG_Athlete.LKickMatch);
            BindControl(ntbK1RulMatch, FG_Athlete.K1RulMatch);

            BindObject(ID, FG_Athlete.ID);

            //dgwCoach.AutoGenerateColumns = false;
            //dgwCoach.DataSource = dbManager.SlaveBinding("Coach");
            //dgwCoach.RowIndex = FG_Coach.Line;

            //BindColumn(clc_Line, FG_Coach.Line);
            //BindColumn(clc_FirstName, FG_Coach.FirstName);
            //BindColumn(clc_Phone, FG_Coach.Phone);

            //BindControl(dgwCoach);
            BindCounter(ID);
        }

        protected override void FocusOnNew()
        {
            //txtClub.Focus();
        }

        protected override void OnDisableControlsForEdit()
        {
            //txtMail.Enabled = false;
            base.OnDisableControlsForEdit();
        }

        protected override bool OnBeforeSave()
        {
            //if (txtMail.Text.IsEmpty())
            //{
            //    MyMessageBox(this, Properties.Resources.M_Warning, Properties.Resources.M_MissingMail, MessageBoxButtons.OK, MessageBoxIcon.Warning );
            //    return false;
            //}

            return base.OnBeforeSave();
        }

        #endregion
    }

    #region DbManager dbmanagerTeam

    /// <summary>
    /// Classe derivata per gestioni particolari
    /// </summary>
    internal class dbmanagerAthlete : DBManager
    {
        public dbmanagerAthlete(string name, RadarForm radar, DocumentForm document)
            : base(name, radar, document)
        { }

        protected override void dAdapter_MasterRowUpdating(object sender, SqlABRowUpdatingEventArgs e)
        {
            base.dAdapter_MasterRowUpdating(sender, e);
        }

        protected override void dAdapter_RowUpdating(object sender, SqlABRowUpdatingEventArgs e)
        {
            //if (e.StatementType == StatementType.Insert)
            //{
            //    if (e.Row.Table.ToString() == FG_Coach.Name)
            //        e.Row[FG_Coach.Mail.Name] = GetColumn<string>(FG_Club.Mail, Pos);
            //}
            base.dAdapter_RowUpdating(sender, e);
        }

        protected override string CreateMasterQuery()
        {
            this.Table = new FG_Athlete();
            QueryBuilder qB = new QueryBuilder();
            qB.AddSelect();
            qB.AddFrom(this.Table);
            qB.AddCompare(FG_Athlete.ID, "@p1");

            return qB.Query;
        }

        protected override List<SqlABParameter> CreateMasterParam()
        {
            List<SqlABParameter> PList = new List<SqlABParameter>();

            SqlABParameter sParam = new SqlABParameter("@p1", FG_Athlete.Mail);
            sParam.Value = FG_Athlete.Mail.DefaultValue;
            PList.Add(sParam);
            return PList;
        }

        protected override List<SqlABParameter> CreateSlaveParam(string name)
        {
            //if (name == FG_Coach.Name)
            //{
            //    List<SqlABParameter> PList = new List<SqlABParameter>();

            //    SqlABParameter sParam = new SqlABParameter("@p2", FG_Coach.Mail);
            //    sParam.Value = "";
            //    PList.Add(sParam);
            //    return PList;
            //}

            return null;
        }

        protected override string CreateSlaveQuery(string name)
        {
            //if (name == FG_Coach.Name)
            //{
            //    QueryBuilder qB = new QueryBuilder();
            //    qB.AddSelect();
            //    qB.AddFrom(FG_Coach.Name);
            //    qB.AddCompare(FG_Coach.Mail, "@p2");
            //    qB.AddOrder(FG_Coach.Line);
            //    return qB.Query;
            //}

            return "";
        }

        protected override void SetParameters(ApplicationLibrary.ExtendedControls.IRadarParameters key, DBCollection collection)
        {
            collection.Parameter[0].Value = key[0];
        }
    }

    #endregion
}